	/*
	 * Copyright (C) 2004 Derek James and Philip Tucker
	 * 
	 * This file is part of ANJI (Another NEAT Java Implementation).
	 * 
	 * ANJI is free software; you can redistribute it and/or modify it under the terms of the GNU
	 * General Public License as published by the Free Software Foundation; either version 2 of the
	 * License, or (at your option) any later version.
	 * 
	 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
	 * the GNU General Public License for more details.
	 * 
	 * You should have received a copy of the GNU General Public License along with this program; if
	 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
	 * 02111-1307 USA
	 * 
	 * created by Philip Tucker on May 17, 2003
	 */

package simmar.neat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.jgap.Chromosome;
import org.jgap.Configuration;

import com.anji.Copyright;
import com.anji.integration.Activator;
import com.anji.integration.ActivatorTranscriber;
import com.anji.integration.TargetFitnessFunction;
import com.anji.persistence.Persistence;
import com.anji.util.Configurable;
import com.anji.util.DummyConfiguration;
import com.anji.util.Properties;
import com.anji.util.Randomizer;

/**
 * Transcribe <code>Chromosome</code> object (loaded from persistence if necessary) into
 * <code>Activator</code> object and activate it with specified stimuli.
 * 
 * @author Philip Tucker
 */
public class NEATActivator {

	private Persistence db;

	private ActivatorTranscriber activatorFactory;

	/**
	 * See <a href=" {@docRoot}/params.htm" target="anji_params">Parameter Details </a> for
	 * specific property settings.
	 * @param props configuration parameters.
	 */
	public void init( Properties props ) {
		try {
			db = (Persistence) props.singletonObjectProperty( Persistence.PERSISTENCE_CLASS_KEY );
			activatorFactory = (ActivatorTranscriber) props.singletonObjectProperty( ActivatorTranscriber.class );
		}
		catch ( Exception e ) {
			throw new IllegalArgumentException( "invalid properties: " + e.getClass().toString() + ": " + e.getMessage() );
		}
	}

	/**
	 * Load chromosome from persistence and activate it.
	 * 
	 * @param chromId persistence ID of chromosome
	 * @return SortedMap contains key Integer index, value double[] response
	 * @throws Exception
	 * @see NeatActivator#activate(Activator)
	 */
	public void activate( String chromId ) throws Exception {
		Configuration config = new DummyConfiguration();
		Chromosome chrom = db.loadChromosome( chromId, config );

		Activator activator = activatorFactory.newActivator( chrom );
		db.store( activator );
		
		if (!NEATGameWrapper.isInitialized()) {
			NEATGameWrapper.initGameWrapper(false);
		}
		int[] seedArray = {13, 42, 115};
		NEATGameWrapper.evaluateActivator(activator, seedArray, true);
	}

	/**
	 * Loads chromosome from persistence, transcribes it into activator, and displays activation.
	 * 
	 * @param args args[0] is properties file name, args[1] is chromosome ID
	 * @throws Exception
	 */
	public static void main( String[] args ) throws Exception {
		System.out.println( Copyright.STRING );
		if ( args.length < 2 )
			return;
		else {
			NEATActivator na = new NEATActivator();
			Properties props = new Properties( args[ 0 ] );
			na.init( props );
			na.activate( args[1] );
		}
	}
}